let uid = 0;

export default class Dep {
  constructor() {
    this.subs = [];
    this.id = uid++;
    this.setIds = new Set();
  }

  depend() {
    Dep.target.addDep(this); //让watcher也收集dep
  }
  addSub(watcher) {
    this.subs.push(watcher);
    // const id = watcher.id;
    // if (!this.setIds.has(id)) {
    //   this.setIds.add(id);
    // }
  }
  addWatcher(watcher) {
    if (!this.setIds.has(watcher.id)) {
      this.subs.push(watcher);
    }
  }
  notify() {
    this.subs.forEach((sub) => {
      sub.update();
    });
  }
}

Dep.target = null;
let targetStack = [];
export function pushTarget(target) {
  Dep.target = target;
  targetStack.push(target)
}

export function popTarget() {
  targetStack.pop();
  Dep.target = targetStack[targetStack.length - 1];
}
